<?php

namespace app\logic;

use yii\base\Model;
use yii\base\BaseObject;
use yii\web\HttpException;
use app\models\ActiveRecord;

class BaseLogic extends BaseObject
{
    /**
     * Notes:
     * Date: 2020/7/24
     * Time: 14:39
     * @param $list
     * @param ActiveRecord $model
     * @return array
     */
    public static function getListReturn($list, $model)
    {
        if (isset($list['list'])) {
            $returnList = array_merge([
                'list' => [],
                'currentCount' => count($list['list']),
                'pageCount' => $model->getPage(),
                'listCount' => $model->getCount(),
                'page' => $_GET['page'],
                'pageSize' => $model->getPageSize()
            ], $list);
        } else {
            $returnList = [
                'list' => $list,
                'currentCount' => count($list),
                'pageCount' => $model->getPage(),
                'listCount' => $model->getCount(),
                'page' => $_GET['page'] ?? 0,
                'pageSize' => $model->getPageSize()
            ];
        }
        return $returnList;
    }

    /**
     * Notes:参数、表单数据 验证
     * Date: 2020/7/24
     * Time: 17:10
     * @param Model $model
     * @param array $params
     * @param string $scenario
     * @return Model
     * @throws HttpException
     */
    public static function _validate($model, $params, $scenario = '')
    {
        $scenario ? $model->setScenario($scenario) : '';
        $model->setAttributes($params);
        if (!$model->validate()) {
            throw new HttpException(423, $model->getFirstError());
        }
        return $model;
    }

}